﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;


namespace Project.module
{
    public partial class frmThemmon : Form
    {
        public MonAnDTO mon;
        public BanDTO ban;
        public int MADM;
        public MonAnDTO Laymon
        {
            get
            {
                return mon;
            }
            set
            {
                mon = value;
            }
        }
        public frmThemmon()
        {
            InitializeComponent();
        }

        private void frmThemmon_Load(object sender, EventArgs e)
        {
            lblTenmon.Text = mon.Tenmon;
            ban = (BanDTO)this.Tag;
           
            MADM = DatMonBUS.GetMaDM(ban.Maban);
            
            
            lblDongia.Text = mon.Dongia.ToString();
            lblThanhtien.Text = String.Format("{0:0,0 VND}", mon.Dongia);
            
        }

        private void txtSoluong_TextChanged(object sender, EventArgs e)
        {
            int num;

            if (int.TryParse(txtSoluong.Text, out num) != false && int.Parse(txtSoluong.Text) > 0 && txtSoluong.Text != "")
            {
                float thanhtien = mon.Dongia * int.Parse(txtSoluong.Text);
                lblThanhtien.Text = String.Format("{0:0,0 VND}", thanhtien);
            }
            else
            {
                lblThanhtien.Text = String.Format("{0:0,0 VND}", mon.Dongia);
            }
            
        }

        private void txtSoluong_MouseLeave(object sender, EventArgs e)
        {
           
        }

        private void bntOk_Click(object sender, EventArgs e)
        {
            CTDatMonDTO ct = new CTDatMonDTO();
            bool kt = false;
            if (MADM == 0)
            {
                DatMonDTO dm=new DatMonDTO();
                dm.Maban=ban.Maban;
                dm.Tinhtrang=1;
                MADM = DatMonBUS.Addnew(dm);
                BanBUS.SetTrangThai(ban.Maban, 1);

            }
            ct.Madm = MADM;
            ct.Mamon = mon.Mamon;
            ct.Soluong = int.Parse(txtSoluong.Text);
            ct.Dongia = int.Parse(lblDongia.Text);
            if (CTDatmonBUS.CheckDatMon(MADM, mon.Mamon)==true)
            {
                kt = CTDatmonBUS.Capnhatdatmon(MADM, mon.Mamon, int.Parse(txtSoluong.Text));
            }
            else
            {
                kt = CTDatmonBUS.Themchitiet(ct);
            }
            if (kt == true)
            {
                MessageBox.Show("Thêm món thành công !","Thành công !",MessageBoxButtons.OK,MessageBoxIcon.Information);
                this.Close();
            }
            else
                MessageBox.Show("Khong thanh cong");

        }

        private void bntHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtSoluong_KeyDown(object sender, KeyEventArgs e)
        {
            int num;
            if (int.TryParse(txtSoluong.Text, out num) == true)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    CTDatMonDTO ct = new CTDatMonDTO();
                    bool kt = false;
                    if (MADM == 0)
                    {
                        DatMonDTO dm = new DatMonDTO();
                        dm.Maban = ban.Maban;
                        dm.Tinhtrang = 1;
                        MADM = DatMonBUS.Addnew(dm);
                        BanBUS.SetTrangThai(ban.Maban, 1);

                    }
                    ct.Madm = MADM;
                    ct.Mamon = mon.Mamon;
                    ct.Soluong = int.Parse(txtSoluong.Text);
                    ct.Dongia = int.Parse(lblDongia.Text);
                    if (CTDatmonBUS.CheckDatMon(MADM, mon.Mamon) == true)
                    {
                        kt = CTDatmonBUS.Capnhatdatmon(MADM, mon.Mamon, int.Parse(txtSoluong.Text));
                    }
                    else
                    {
                        kt = CTDatmonBUS.Themchitiet(ct);
                    }
                    if (kt == true)
                    {
                        MessageBox.Show("Thêm món thành công !", "Thành công !", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    else
                        MessageBox.Show("Khong thanh cong");

                }
            }
            
        }
    }
}
